package com.shuimin.service;

import com.shuimin.service.model.sys.User;
import com.shuimin.service.service.sys.SysRoles;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import pond.common.S;
import pond.db.Record;
import pond.security.rbac.RBAC;
import pond.web.*;
import pond.web.http.HttpMethod;

import java.util.List;


/**
 * 系统管理-员工登陆登出等...
 * Created by sky on 15/10/31.
 */
public class SYSTEM extends Controller {

  public static final Logger logger = LoggerFactory.getLogger(SYSTEM.class);


  @Mapping("/menu")
  public void menu(Request req, Response resp) {

    RBAC.Roles roles = App.currentRoles();
    List<Record> menus = S._for(SysRoles.menus)
        .filter(record -> roles.hasEvery((String) record.get("id")))
        .toList();
    resp.render(Render.json(menus));
  }

  @Mapping(value = "/login_user", methods = HttpMethod.GET)
  public void getLoginUser(Request req, Response resp) {
    resp.render(Render.json(((User) Session.get(req).get(App.LOGIN_USER))
                                .refineViewShops()
                                .refineRoles()
                                .refineShop()));
  }

  @Mapping(value = "/logout", methods = HttpMethod.POST)
  public void logout(Request req, Response resp) {
    Session.get(req).invalidate();
    resp.send(204);
//    resp.redirect("/");
  }

}
